/*
 * add: 流程地图  by Chui
 * dataTime: 2014-05-27
 */
(function($){
$.widget("kit.flowProgress",{
    options:{
        position: {
            my:"left top+5",
            at:"left bottom"
        },
        time: 100
    },
    _create: function(){
        var target = $(this.element),
            table = target.find("table"),
            tableWrapper = target.find(".tipsDiv"),
            tipsArrow = target.find( ".tips-arrow"),
            options = this.options,
            bottomm = true;
        var timer = [];
        table.find("tbody tr:odd").addClass("odd").end().find("tbody tr:even").addClass("even");

        target.each(function(i){

                $(this).on({
                    "mouseenter" : function(event){
                        if(table.size()){
                            if(!tableWrapper.size()){
                                tableWrapper = $("<div>").addClass("tipsDiv").appendTo(target);
                                table.appendTo(tableWrapper);
                                tipsArrow = $("<i class='tips-arrow'></i>").appendTo( target );
                            };
                            clearTimeout(timer[i]);

                            var _y = $( "body").outerHeight() - target.offset().top - 47,
                                _h = tableWrapper.outerHeight();

                            tableWrapper.appendTo(target.parents( ".flowProgress" )).hide().fadeIn( 300 );
                            tipsArrow.appendTo(target.parents( ".flowProgress" )).hide().fadeIn( 300 );
                            if( _y > _h ){
                                tableWrapper.position({
                                    my:options.position.my,
                                    at:options.position.at,
                                    of:target
                                });
                                tipsArrow.position({
                                    my:"left+20 top-2",
                                    at:"left bottom",
                                    of:target
                                });
                            }else{
                                tableWrapper.position({
                                    my:"left bottom-8",
                                    at:"left top",
                                    of:target
                                });
                                tipsArrow.addClass( "tips-arrow-down" ).position({
                                    my:"left+20 bottom-1",
                                    at:"left top",
                                    of:target
                                });
                            }
                        }
                    },
                    "mouseleave": function(){

                        timer[i] = setTimeout(function(){
                            tableWrapper.appendTo(target).hide();
                            tipsArrow.appendTo(target).hide();
                        },options.time);
                        tableWrapper.on({
                            "mouseenter": function(){
                                clearTimeout(timer[i]);
                            },
                            "mouseleave": function(){
                                timer[i] = setTimeout(function(){
                                    tableWrapper.appendTo(target).hide();
                                    tipsArrow.appendTo(target).hide();
                                },options.time);
                            }
                        })
                    }
                })
        });

    }
});

$.fn.tabIframes = function(opts){
        var defaults = {
        		form:'#queryForm'
        },
            options = $.extend({},defaults,opts);

        return this.each(function(){
            var links = $(this).find("a"),
                iframesContainer = $("#iframesContainer"),
                tabBtns = $(this).find(".tabBtn");

            // 显示下拉菜单
            var mask;
            tabBtns.on({
                "mouseenter": function(){
                    var subtabs = $(this).find(".subtabs");
                    subtabs.show();
                    mask = $("<div>").addClass("tabMask").css({
                        width: subtabs.outerWidth()+"px",
                        height: subtabs.outerHeight()+"px",
                        opacity:.2
                    }).appendTo($(this));
                },
                "mouseleave": function(){
                    $(this).find(".subtabs").hide();
                    mask.remove();
                }
            })

            links.each(function(i){
                $(this).click(function(){
                    var link = setUrlParams($(this).data("link"),options.form),
                        iframes = iframesContainer.find("iframe"),
                        iframe = $("#ifm"+i);

                    // 目标iframe已经为显示的情况
                    if($(this).hasClass("active") || $(this).parent().hasClass("active")){
                        return false;
                    }

                    // 目标iframe隐藏,且点击的是 tabBtn
                    if($(this).parent().hasClass("tabBtn")){
                        $(this).parent().addClass("active").siblings().removeClass("active");
                        iframes.filter(":visible").hide();
                        if($(this).siblings(".subtabs").size()){ // 点击切换subtabs下面的iframe
                            var subindex
                            links.each(function(n){
                                if($(this).text() == $("#subtab").text() && $(this).parent().is(".subtabs")){
                                    subindex = n;
                                    link = setUrlParams($(this).data("link"),options.form);
                                }
                            });
                            iframe = $("#ifm"+subindex);
                            i = subindex;
                            if(iframe.size()){
                                iframe.show();
                                return false;
                            }
                            $("<iframe id='ifm"+i+"' name='flowProgress"+i+"' frameborder='0' height='770' width='100%' scrolling='no' src='"+link+"'></iframe>").appendTo(iframesContainer);

                            return false;
                        }
                        if(iframe.size()){
                            iframe.show();
                            return false;
                        }
                        $("<iframe id='ifm"+i+"' name='flowProgress"+i+"' frameborder='0' height='770' width='100%' scrolling='no' src='"+link+"'></iframe>").appendTo(iframesContainer);
                    }

                    // 目标iframe隐藏,且点击的是 subtabs
                    if($(this).hasClass("subtab")){
                        $(this).addClass("active").siblings().removeClass("active");
                        iframes.filter(":visible").hide();
                        $("#subtab").text($(this).text());
                        $(this).parents(".tabBtn").addClass("active").siblings().removeClass("active");
                        if(iframe.size()){
                            iframe.show();
                            return false;
                        }
                        $("<iframe id='ifm"+i+"' name='flowProgress"+i+"' frameborder='0' height='770' width='100%' scrolling='no' src='"+link+"'></iframe>").appendTo(iframesContainer);
                    }
                })
            })


        })
    }

/*
 * fp-* 自动适应内容
 * 应用: 数据中心配套开发合同签订流程过程
 */
function eleAnimate(ele,rows,min_width){
    var target = ele,
        eleWrapper = target.parents(".fp-ele-wrapper"),
        max_width = rows*min_width,
        width = max_width/2,
        timer = "",
        targetWrapper = eleWrapper.find(".title-wrapper"),
        rBg = targetWrapper.siblings(".r-bg"),
        more = "";
    target.css({
        width: min_width+"px",
        height:"32px",
        overflow:"hidden",
        top:"5px"
    });
    more = $("<i class='more'>...</i>").appendTo(target);
    target.on({
        "mouseenter": function(){
            timer = setTimeout(function(){
                more.hide();
                targetWrapper.add(target).animate({
                    width: width+"px"
                },300);
                rBg.animate({
                    left: width+56+"px"
                },300);
            },300)
        },
        "mouseleave": function(){
            if(timer){
                clearTimeout(timer);
            }
            targetWrapper.add(target).animate({
                width: min_width+"px"
            },300,function(){
                more.show();
            });
            rBg.animate({
                left: min_width+50+"px"
            },300)
        }
    })

}
$.fn.fpAnimateWidth = function(opts){
    var defaults = {},
       options = $.extend({},defaults,opts);
    return this.each(function(){
        var rows = $(this).find(".fp-row"),
            rows_size = rows.size();
        rows.each(function(i){
            var eleWrapper = $(this).find(".fp-ele-wrapper"),
                eleWrapper_size = eleWrapper.size(),
                _rows = $(this);

            // 添加向下箭头
            if(i%2 == 0){
                $("<i class='fp-arrow-down'></i>").appendTo($(this));
            }else {
                $(this).addClass("odd");
                $("<i class='fp-arrow-down'></i>").css("left","60px").appendTo($(this));
                //eleWrapper.eq(0).css("margin-left",(4-eleWrapper_size)*239+"px");
            }
            rows.eq(rows_size-1).find(".fp-arrow-down").remove();

            // 为单项指标
            eleWrapper.each(function(i){
                var tips = $(this).find(".tips-content"),
                    tipsWrapper = "",
                    ele = $(this).find(".fp-ele"),
                    title = ele.find(".title"),
                    title_h = title.outerHeight(),
                    min_title_w = title.outerWidth(),
                    title_w = "",
                    title_rows = title_h/16,
                    _eleWrapper = $(this);

                // 设置title的样式
                title.wrap("<div class='title-wrapper'></div>");
                switch(title_rows){
                    case 1:
                        title.css("top","14px");
                        break;
                    case 2:
                        title.css("top","5px");
                        break;
                    default:
                        eleAnimate(title,title_rows,80);
                }

                // 设置弹出提示层样式
                if(tips.size()){
                    tipsWrapper = $("<div class='tipsDiv'></div>").appendTo($(this));
                    $("<i class='flowArrow'></i>").add(tips).appendTo(tipsWrapper);
                    title.on({
                        "mouseenter": function(){
                            tipsWrapper.show();
                            _eleWrapper.parent().add(_eleWrapper).css("z-index",100);
                        },
                        "mouseleave": function(){
                            tipsWrapper.hide();
                            _eleWrapper.parent().add(_eleWrapper).css("z-index",1);
                        }
                    })
                }

                // 为单项指标设置箭头
                if(_rows.hasClass("odd")){
                    if(i == 0){
                        $(this).css("margin-right","20px");
                    }else{
                        $("<i class='flowArrow'></i>").appendTo($(this));
                    }
                }else{
                    if(i == (eleWrapper_size - 1)){
                        $(this).css("margin-right","0");
                    }else{
                        $("<i class='flowArrow'></i>").appendTo($(this));
                    }
                }

            })

        })
    })
}

function setIframeHeight( target ){
    var iframe = target,
        height = $( "body" ).outerHeight();
    target.attr( "height", height );
}

})(jQuery);


